<?php
class SqlDataProvider implements IDataProvider {
	/*
	 * (non-PHPdoc) @see IDataProvider::provide()
	 */
	public function provide($params) {
		$fields = isset ( $params ['fields'] ) && ! empty ( $params ['fields'] ) ? $params ['fields'] : '*';
		$from = isset ( $params ['from'] ) && ! empty ( $params ['from'] ) ? $params ['from'] : false;
		$where = isset ( $params ['where'] ) && ! empty ( $params ['where'] ) ? $params ['where'] : false;
		$order = isset ( $params ['order'] ) && ! empty ( $params ['order'] ) ? $params ['order'] : false;
		$opt ['limit'] = isset ( $params ['limit'] ) && ! is_numeric ( $params ['limit'] ) ? $params ['limit'] : 10;
		$opt ['start'] = isset ( $params ['start'] ) && ! is_numeric ( $params ['start'] ) ? $params ['start'] : 0;
		$having = isset ( $params ['having'] ) && ! empty ( $params ['having'] ) ? $params ['having'] : false;
		if (! $from) {
			return new CtsDataSet ( array () );
		}
		$sql = "SELECT {$fields} FROM {$from}";
		if ($where) {
			$sql .= " WHERE {$where}";
		}
		if ($order) {
			$sql .= " ORDER BY {$order}";
		}
		if ($having) {
			$sql .= " HAVING {$having}";
		}
		$pM = new WebPageModel ();
		
		$rst = $pM->query ( $sql, $opt );
		
		if (! $rst) {
			return new CtsDataSet ( $rst );
		} else {
			return new CtsDataSet ( array () );
		}
	}
}